﻿using HNCSolutions.Models;
using HNCSolutions.ViewModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HNCSolutions.Areas.Admin.Controllers
{
    public class PhanMemController : Controller
    {
        private HNCSolutionsEntities db = new HNCSolutionsEntities();
        //
        // GET: /Admin/PhanMem/

        public ActionResult Index(int currentPage = 1, int pageRows = 2)
        {
            var listPhanMem = db.PhanMems.OrderBy(sp => sp.Id).Skip((currentPage - 1) * pageRows).Take(pageRows);
            int totalPages = (db.PhanMems.Count() / pageRows) + (db.PhanMems.Count() % pageRows > 0 ? 1 : 0);
            ListPhanMemViewModel viewModel = new ListPhanMemViewModel
            {
                ListPhanMem = listPhanMem,
                Paging = new Paging { CurrentPage = currentPage, PageRows = pageRows, TotalPage = totalPages }
            };

            return View(viewModel);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(PhanMemViewModel viewModel)
        {
            HNCSolutions.Models.HNCSolutionsEntities entities = new HNCSolutionsEntities();
            entities.PhanMems.Add(viewModel.PhanMem);
            entities.SaveChanges();
            return this.Index();
        }

        public ActionResult Edit(int Id)
        {
            HNCSolutions.Models.HNCSolutionsEntities entities = new HNCSolutionsEntities();
            var phanMem = entities.PhanMems.Single(pm => pm.Id == Id);
            PhanMemViewModel viewModel = new PhanMemViewModel { PhanMem = phanMem };
            return View(viewModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(PhanMemViewModel viewModel)
        {
            HNCSolutions.Models.HNCSolutionsEntities entities = new HNCSolutionsEntities();
            entities.Entry(viewModel.PhanMem).State = EntityState.Modified;
            entities.SaveChanges();
            return this.Index();
        }
    }
}
